package cn.fintecher.gis.web;


import cn.fintecher.gis.annotation.CurrentOperator;
import cn.fintecher.gis.entity.SupplyInfo;
import cn.fintecher.gis.entity.UploadFile;
import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.model.request.FlowApproveRequest;
import cn.fintecher.gis.model.request.FlowBusinessRequest;
import cn.fintecher.gis.model.request.FlowNextUserRequest;
import cn.fintecher.gis.model.response.*;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.service.FlowService;
import cn.fintecher.gis.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/flowInfoController")
@Api(value = "flowInfoController", description = "流程相关接口")
public class FlowInfoController {

    @Autowired
    private FlowService flowService;


    @GetMapping("/queryFlowInfo")
    @ApiOperation(value = "查询流程(新建、待审批、已审批、打回)信息", notes = "查询流程(新建、待审批、已审批、打回)信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FlowBusinessResponse>> queryFlowInfo(Pageable pageable,
                                                                    FlowBusinessRequest request,
                                                                    @ApiIgnore @CurrentOperator SessionModel sessionModel) {
        List<FlowBusinessResponse> list = flowService.getFlowBusiness(request, sessionModel);
        //按照申请时间降序排序
        list.stream().sorted(Comparator.comparing((FlowBusinessResponse::getCreateTime)).reversed()).collect(Collectors.toList());
        Page<FlowBusinessResponse> page = PageUtil.listConvertToPage(list, pageable);
        return ResponseEntity.ok().body(page);
    }


    @GetMapping("/querySupplyInfoById")
    @ApiOperation(value = "查询相关的供地信息", notes = "查询相关的供地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<SupplyInfoResponse> querySupplyInfoById(String id) {
        SupplyInfoResponse supplyInfoResponse = flowService.querySupplyInfoById(id);
        return ResponseEntity.ok().body(supplyInfoResponse);
    }



    @GetMapping("/queryFlowInfoFinish")
    @ApiOperation(value = "查询归档信息", notes = "查询归档信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FlowBusinessResponse>> queryFlowInfoFinish(Pageable pageable,
                                                                          FlowBusinessRequest request) {
        List<FlowBusinessResponse> list = flowService.getFlowBusinessResponseFinish(request);
        Page<FlowBusinessResponse> page = PageUtil.listConvertToPage(list, pageable);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/getFlowChart")
    @ApiOperation(value = "根据流程id查询流程图", notes = "根据流程id查询流程图")
    public ResponseEntity<List<FlowChartResponse>> getFlowChart(@RequestParam("flowId") String flowId){
        List<FlowChartResponse> list = flowService.getFlowChart(flowId);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/getBusinessFileByFlowId")
    @ApiOperation(value = "根据流程id查询对应的业务文件信息", notes = "根据流程id查询对应的业务文件信息")
    public ResponseEntity<List<UploadFile>> getBusinessFileByFlowId(@RequestParam("flowId") String flowId){
        List<UploadFile> list = flowService.getBusinessFileByFlowId(flowId);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/getLayerInfoResponse")
    @ApiOperation(value = "根据流程id查询流图层信息", notes = "根据流程id查询流图层信息")
    public ResponseEntity<LayerInfoResponse> getLayerInfoResponse(@RequestParam("flowId") String flowId){
        LayerInfoResponse layerInfoResponse = flowService.getLayerInfoResponse(flowId);
        return ResponseEntity.ok().body(layerInfoResponse);
    }

    @GetMapping("/getBusinessInfoResponseByFlowId")
    @ApiOperation(value = "根据流程id查询流基础信息", notes = "根据流程id查询流基础信息")
    public ResponseEntity<BusinessInfoResponse> getBusinessInfoResponseByFlowId(@RequestParam("flowId") String flowId){
        BusinessInfoResponse businessInfoResponse = flowService.getBusinessInfoResponseByFlowId(flowId);
        return ResponseEntity.ok().body(businessInfoResponse);
    }


    @GetMapping("/getFlowNumberResponse")
    @ApiOperation(value = "获取当前用户名称流程分类数量", notes = "获取当前用户名称流程分类数量")
    public ResponseEntity<FlowNumberResponse> getFlowNumberResponse(@ApiIgnore @CurrentOperator SessionModel sessionModel) {
        FlowNumberResponse response = flowService.getFlowNumberResponse(sessionModel);
        return ResponseEntity.ok().body(response);
    }

    @PutMapping("/finishFlow/{id}")
    @ApiOperation(value = "结束流程", notes = "结束流程")
    public ResponseEntity<Void> finishFlow(@PathVariable String id) {
        flowService.finishFlow(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/setFlowNextUser")
    @ApiOperation(value = "指定下一个审批人", notes = "指定下一个审批人")
    public ResponseEntity<Void> setFlowNextUser(@RequestBody FlowNextUserRequest request,
                                                @ApiIgnore @CurrentOperator SessionModel sessionModel) {
        flowService.setFlowNextUser(request, sessionModel);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/flowApproval")
    @ApiOperation(value = "流程审批", notes = "流程审批")
    public ResponseEntity<Void> flowApproval(@RequestBody FlowApproveRequest request){
        flowService.flowApproval(request);
        return ResponseEntity.ok().build();
    }



}
